﻿using System;

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Reports;
using DevExpress.ExpressApp.Updating;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.Security;
using DevExpress.ExpressApp.Security.Strategy;

using WaTP.Module.BusinessObjects;

namespace WaTP.Module.DatabaseUpdate
{
    public class Updater : ModuleUpdater
    {
        public Updater(IObjectSpace objectSpace, Version currentDBVersion) : base(objectSpace, currentDBVersion) { }
        public override void UpdateDatabaseAfterUpdateSchema()
        {
            base.UpdateDatabaseAfterUpdateSchema();

            #region Thiết lập phân loại Nhập kho, xuất kho
            OrderCategory importOrder = ObjectSpace.FindObject<OrderCategory>(
                CriteriaOperator.Parse("OrderName == 'Import'"));
            if (importOrder == null)
            {
                importOrder = ObjectSpace.CreateObject<OrderCategory>();
                importOrder.OrderName = "Import";
                importOrder.Save();
            }
            OrderCategory exportOrder = ObjectSpace.FindObject<OrderCategory>(
                CriteriaOperator.Parse("OrderName == 'Export'"));
            if (exportOrder == null)
            {
                exportOrder = ObjectSpace.CreateObject<OrderCategory>();
                exportOrder.OrderName = "Export";
                exportOrder.Save();
            }
            #endregion
            #region Loại nhập/xuất của kho hàng.
            
            #endregion
            #region Administrator
            SecuritySystemRole adminRole = ObjectSpace.FindObject<SecuritySystemRole>(
                new BinaryOperator("Name", SecurityStrategy.AdministratorRoleName));
            if (adminRole == null) { 
                adminRole = ObjectSpace.CreateObject<SecuritySystemRole>();
                adminRole.Name = SecurityStrategy.AdministratorRoleName;
                adminRole.IsAdministrative = true;
            }
            SecuritySystemUser user1 = ObjectSpace.FindObject<SecuritySystemUser>(
                new BinaryOperator("UserName", "Sam"));
            if (user1 == null) { 
                user1 = ObjectSpace.CreateObject<SecuritySystemUser>();
                user1.UserName = "Admin";
                //Set a password if the standard authentication type is used.
                user1.SetPassword("tancojsc123!@#");
            }
            user1.Roles.Add(adminRole);
            #endregion

            #region Report
            CreateReport("TransferOrderReportCompleted");
            CreateReport("TransferOrderReportInprogress");
            CreateReport("TransferOrderI");
            CreateReport("TransferOrderII");
            CreateReport("TransferOrderIII");
            CreateReport("WarningProduct");
            CreateReport("CustomsFormalityCompleted");
            CreateReport("CustomsFormalityInprogress");
            #endregion
        }

        private void CreateReport(string reportName) {
            ReportData reportdata = ObjectSpace.FindObject<ReportData>(
                new BinaryOperator("Name", reportName));
            if (reportdata == null) { 
                reportdata = ObjectSpace.CreateObject<ReportData>();
                XafReport rep = new XafReport();
                rep.ObjectSpace = ObjectSpace;
                rep.LoadLayout(GetType().Assembly.GetManifestResourceStream(
                    "WaTP.Module.EmbeddedReports." + reportdata + ".repx"));
                rep.ReportName = reportName;
                reportdata.SaveReport(rep);
            }
        }
    }
}
